<?php

class HasAndBelongsToManyAssociation extends Association
{

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/Association#loadData($client)
   */
  protected function loadData(IAggregate $client)
  {
    $def = $this->getDefinition();
    
    $dao = DAO::getInstance();

    $id = $client->getId();

    $atable = $def->getAssociationTable();
    $afk = $def->getAssociationForeignKey();
    $apk = $def->getAssociationPrimaryKey();

    $table = $def->getTarget()->getTable();
    $fk = $def->getForeignKey();
    $pk = $def->getPrimaryKey();

    $fields = Repository::collectFields($def->getTarget());

    $query = $dao->query()->from($table)->join("$atable ON $atable.$afk = $table.$fk")->select($fields)->where("$pk = ?");

    $result = $query->execute($id);

    $objs = DomainMapper::inflateCollection($this->getDefinition()->getTarget(), $result->fetchAll());

    $meta = & $this->data($client);
    $meta['value'] = $objs;
  }

}

?>